<?php

namespace app\adminapi\controller;

use think\Controller;
use think\Request;

class Categorys extends BaseApi
{
   public function index(){
        // 接受pid参数 影响查询的数据
        // $pid = input('pid','');
        // if($oid === ''){}
        $param = input();
        if(isset($param['pid'])){
            $where['pid']=$param['pid'];
        }
        // 接受type参数 影响返回的数据

        // 查询数据
        $list = \app\adminapi\model\Category::where($where)->select();
        
        $list = (new \think\Collection($list))->toArray();
        // if(isset($param['type']) && $param['type']=='list'){

        // }else{
        //     $list = get_cate_list($list);
        // }
        if(!isset($param['type']) || $param['type']!='list'){
            // 转换成无限级分类列表
            $list = get_cate_list($list);
        }
        // 返回数据
        $this->ok($list);
   }
   public function read($id){
       // 查询数据
        $info = \app\adminapi\model\Category::find($id);
       // 返回数据
        $this->ok($info);
    }
    public function save(Request $request){
        // 接收参数
        $param = input();
        // 参数检测
        $validate = $this->validate($param,[
            'cate_name'=>'require|length:2,20',
            'pid'=>'require|integer|egt:0',
            'is_show'=>'require|in:0,1',
            'is_hot'=>'require|in:0,1',
            'sort'=>'require|between:0,9999',
        ]);
        if($validate!==true){
            $this->fail($validate);
        }
        // 添加数据（处理pid_path pid_path_name level）
        if($param['pid']==0){
            // 顶级分类
            $param['pid_path']=0;
            $param['pid_path_name']='';
            $param['level']=0;
        }else{
            // 不是顶级分类，查询其上级分类
             $p_info = \app\adminapi\model\Category::where('id',$param['pid'])->find();
             if(empty($p_info)){
                 // 没查到父级
                 $this->fail('数据异常,请稍后再试');
             }
             $param['pid_path'] = $p_info['pid_path'].'_'.$p_info['id'];
             $param['pid_path_name'] = $p_info['pid_path_name'].'_'.$p_info['cate_name'];
             $param['level'] = $p_info['level']+1;
        }
        // logo图片处理
        $param['iamge_url'] = $param['logo'];
        $cate = \app\adminapi\model\Category::create($param,true);
        $info = \app\adminapi\model\Category::find($cate['id']);
        // 返回数据
        $this->ok($info);
    }
    public function update(Request $request,$id){
        // 接收参数
        $param = input();
        // 参数检测
        $validate = $this->validate($param,[
            'cate_name'=>'require|length:2,20',
            'pid'=>'require|integer|egt:0',
            'is_show'=>'require|in:0,1',
            'is_hot'=>'require|in:0,1',
            'sort'=>'require|between:0,9999',
        ]);
        if($validate!==true){
            $this->fail($validate);
        }
        // 修改参数（处理pid_path pid_path_name level image_url）
        if($param['pid']==0){
            // 顶级分类
            $param['pid_path']=0;
            $param['pid_path_name']='';
            $param['level']=0;
        }else{
            // 不是顶级分类，查询其上级分类
             $p_info = \app\adminapi\model\Category::where('id',$param['pid'])->find();
             if(empty($p_info)){
                 // 没查到父级
                 $this->fail('数据异常,请稍后再试');
             }
             $param['pid_path'] = $p_info['pid_path'].'_'.$p_info['id'];
             $param['pid_path_name'] = $p_info['pid_path_name'].'_'.$p_info['cate_name'];
             $param['level'] = $p_info['level']+1;
        }
        if(isset($param['logo']) && !empty($param['logo'])){
            $param['image_url'] = $param['logo'];
        }
        \app\adminapi\model\Category::update($param,['id'=>$id],true);
        // 返回数据
        $info = \app\adminapi\model\Category::find($id);
        $this->ok($info); 
    }
    public function delete($id){
        // 删除数据
        // 判断分类下是否有子分类
        $total = \app\adminapi\model\Category::where('pid',$id)->count();
        if($total>0){
            $this->fail('分类下有子分类，无法删除');
        }
        \app\adminapi\model\Category::destroy($id);
        // 返回数据
        $this->ok();

    }
}   
